package com.chy.edu.controller;


import com.chy.commonutils.Result;
import com.chy.edu.pojo.EduCourse;
import com.chy.edu.pojo.vo.CourseInfoVo;
import com.chy.edu.pojo.vo.CoursePublishVo;
import com.chy.edu.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author chy
 * @since 2020-10-27
 */
@Api(tags = "课程管理")
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    @ApiOperation(value = "课程列表")
    @GetMapping("getCourseList")
    public Result getCourseList() {
        List<EduCourse> list = courseService.list(null);
        return Result.ok().data("list",list);
    }

    @ApiOperation(value = "新增课程")
    @PostMapping("addCourseInfo")
    public Result addCourseInfo(@ApiParam(name = "CourseInfoVo", value = "课程基本信息", required = true)
                                    @RequestBody CourseInfoVo courseInfoVo)
    {
        // 返回添加之后课程id，为了后面添加大纲使用
        String id = courseService.saveCourseInfo(courseInfoVo);
        return Result.ok().data("courseId",id);
    }

    @ApiOperation(value = "根据课程ID查询课程基本信息")
    @GetMapping("getCourseInfo/{courseId}")
    public Result getCourseInfo(@ApiParam(name = "courseId", value = "章节ID", required = true) @PathVariable String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseId(courseId);
        return Result.ok().data("courseInfoVo",courseInfoVo);
    }

    @ApiOperation(value = "修改课程信息")
    @PostMapping("updateCourseInfo")
    public Result updateCourseInfo(@ApiParam(name = "CourseInfoVo", value = "课程基本信息", required = true)
                                       @RequestBody CourseInfoVo courseInfoVo)
    {
        courseService.updateCourseInfo(courseInfoVo);
        return Result.ok();
    }

    @ApiOperation(value = "根据课程id查询课程发布信息")
    @GetMapping("getPublishCourseInfo/{id}")
    public Result getPublishCourseInfo(@ApiParam(name = "id", value = "课程ID", required = true)
                                           @PathVariable String id)
    {
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return Result.ok().data("publishCourse",coursePublishVo);
    }

    @ApiOperation(value = "修改课程状态")
    @PostMapping("publishCourse/{id}")
    public Result publishCourse(@ApiParam(name = "id", value = "课程ID", required = true)
                                    @PathVariable String id)
    {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal"); // 设置课程发布状态
        courseService.updateById(eduCourse);
        return Result.ok();
    }

    @ApiOperation(value = "删除课程")
    @DeleteMapping("{courseId}")
    public Result deleteCourse(@ApiParam(name = "courseId", value = "课程ID", required = true)
                                   @PathVariable String courseId) {
        courseService.removeCourse(courseId);
        return Result.ok();
    }
}

